/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/home/tmp.html', 'timer', 'iscroll', 'dot'], function (page, timer) {

    var render = template.compile(page);
    var HOMEPAGE_CACHE_KEY = 'CacheTmp_Home';

    var apiList = {
        getInfo: api + 'homepage'
    };

    var banner = {
        scroller: null,
        ratio: 960 / 480,
        init: function (length) {
            $('.banner', vm.$.banner).width(db.w).height(db.w / this.ratio);
            $(vm.$.banner).width(db.w).height(db.w / this.ratio);
            $(vm.$.banner).find('.slider').width(db.w * length);

            this.scroller = new IScroll('.bannerArea', {
                snap: true,
                tap: true,
                scrollX: true,
                scrollY: false
            });
        }
    };

    var vm = new Page.creat({
        $id: 'home',
        $model: {
            data: {},
            $: {},
            getHomeData: function () {

                //预渲染
                vm.$init({
                    setTitle: '商城'
                });

                var preview = db.get(HOMEPAGE_CACHE_KEY);
                if (!preview) {
                    vm.data = {
                        banner: [],
                        sec_kill: [],
                        hot_sale: [],
                        machine: [],
                        machine_brand: [],
                        pigment: [],
                        category: [],
                        preview: true
                    };
                    preview = render(vm.data);
                }
                $('#page').html(preview);

                ex.jsonp({
                    url: apiList.getInfo,
                    success: function (obj) {
                        if (!vm.isThisPage())return;
                        console.log(obj);
                        if (obj.code != 0)return '';//todo 跳到错误页去!
                        obj = obj.data;

                        vm.data = {
                            banner: obj.banner || [],
                            sec_kill: obj.sec_kill || [],
                            hot_sale: obj.hot_sale || [],
                            machine: obj.machine || [],
                            machine_brand: obj.machine_brand || [],
                            pigment: obj.pigment || [],
                            category: obj.category || [],
                            now: new Date().getTime()
                        };
                        console.log(obj.banner);

                        var tmp = render(vm.data);
                        $('#page').html(tmp);
                        db.set(HOMEPAGE_CACHE_KEY, tmp);
                        //缓存模版

                        ex.async(function () {

                            vm.bindEvent(vm.event);
                            vm.bind$(vm.$);

                            $('.tag-timer').each(function () {
                                var $this = $(this);
                                var start = $this.attr('data-time');

                                var id = timer.register($this, function (time) {
                                    var t = start - (time - this.creatTime);
                                    if (t < 0) {
                                        timer.logout(id);
                                        this.$dom.find('span').html('已过期');
                                    } else {
                                        this.$dom.find('span').html(template.compile("{{t | formatTime:'hh:mm:ss'}}")({t: t}))
                                    }
                                })
                            });

                            $('.item p').dotdotdot({});
                            $('.hot-item p').dotdotdot({});

                            //new IScroll('#home', {
                            //    scrollX: false,
                            //    scrollY: true
                            //});


                            if (vm.data.hot_sale.length > 0)
                                new IScroll('.hotsaleArea', {
                                    scrollX: true,
                                    scrollY: false
                                });

                            var ratio = 322 / 248;
                            var $c_item = $('.categoryArea .item>div');
                            $c_item.width($c_item.width()).height($c_item.width() / ratio);

                            banner.init(vm.data.banner.length);
                            vm.getLastPosition();

                        });
                    }
                })
            },
            event: {
                bannerSkip: function (url) {
                    location.href = url;
                }
            }
        }
    });

    return {
        init: function () {
            vm.getHomeData();
        }
    }
})
;